mod format;

use rand::Rng;
use std::cmp::Ordering;
use std::io;

fn main() {
    println!("Guess the number !");

    let mut guess = String::new();
    // 生成一个随机数
    let secret_number = rand::thread_rng().gen_range(0..101);
    println!("secret number : {}", secret_number);

    loop {
        // 清空输入 guess 变量
        guess.clear();
        // 接收用户输入
        println!("Please input your  number !");
        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");
        println!("You guessed: {}", guess);

        // guess.trim().parse().expect("Please type a number!");
        // guess.trim().parse::<i32>().expect("Please type a number!");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Please type a number!");
                continue;
            }
        };

        match guess.cmp(&secret_number) {
            Ordering::Less => {
                println!("Too small");
            }
            Ordering::Greater => {
                println!("Too big!");
            }
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }

        // match guess.trim().parse::<i32>() {
        //     Ok(num) => {
        //         if num == secret_number {
        //             println!("You win!");
        //         } else if num > secret_number {
        //             println!("Too big!");
        //         } else {
        //             println!("Too small!");
        //         }
        //     }
        //     Err(_) => println!("Please input a number!"),
        // }
    }
}
